
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author arcsight7
 */
public class MovingBackground {

    Image texture;
    Vector2[] positions;
    int speed;
    int textureWidth;

    public void init(Image texture, int screenWidth, int speed) {
        this.texture = texture;
        this.speed = speed;
        textureWidth = new ImageIcon(texture).getIconWidth();
        positions = new Vector2[screenWidth / textureWidth + 1];
        for (int i = 0; i < positions.length; i++) {
            positions[i] = new Vector2(i * textureWidth, 0);

        }
    }

    public void update() {
        for (int i = 0; i < positions.length; i++) {
            positions[i].x += speed;
            if (speed <= 0) {
                if (positions[i].x <= -textureWidth) {
                    positions[i].x =
                            textureWidth * (positions.length - 1);
                }
            } else {
                if (positions[i].x
                        >= textureWidth * (positions.length - 1)) {
                    positions[i].x = textureWidth;
                }
            }
        }
    }

    public void draw(Graphics g) {
        for (int i = 0; i < positions.length; i++) {
            g.drawImage(texture,
                    positions[i].x, positions[i].y,
                    Color.WHITE, null);
        }
    }
}
